package com.restaurant.springboot.service.impl;

import com.restaurant.springboot.entity.CouponSeriesEntity;
import com.restaurant.springboot.entity.dto.CouponSeriesDTO;
import com.restaurant.springboot.repositories.CouponSeriesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.beans.Transient;
import java.util.List;

@Service
public class CouponSeriseServiceImpl implements com.restaurant.springboot.service.CouponSeriseService {

    @Autowired
    private CouponSeriesRepository couponSeriesRepository;

    @Override
    @Transient
    public List<CouponSeriesEntity> getAllCouponSeries(){
        return couponSeriesRepository.findAllByEnabled(true);
    }

    @Override
    @Transient
    public boolean insertCouponSeries(CouponSeriesDTO couponSeriesDTO){
        if(couponSeriesRepository.save(couponSeriesDTOToPO(couponSeriesDTO))!=null){
            return true;
        }
        return false;
    }

    private CouponSeriesEntity couponSeriesDTOToPO(CouponSeriesDTO couponSeriesDTO){
        return new CouponSeriesEntity(
                couponSeriesDTO.getName(),couponSeriesDTO.getQuota(),couponSeriesDTO.getMixLimit(),
                couponSeriesDTO.getStartDate(),couponSeriesDTO.getEndDate(),couponSeriesDTO.getEffectiveTime(),true
                );
    }

    @Override
    @Transient
    public void deleteCouponSeries(CouponSeriesEntity couponSeriesEntity){
        couponSeriesEntity.setEnabled(false);
        couponSeriesRepository.save(couponSeriesEntity);
    }

    @Override
    public boolean isExistsCouponSeries(int couponSeriesId){
        return couponSeriesRepository.existsById(couponSeriesId);
    }

    @Override
    public CouponSeriesEntity getCouponSeriesById(int couponSeriesId){
        return couponSeriesRepository.findById(couponSeriesId).get();
    }

}
